package com.bysj.money_diary;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import lecho.lib.hellocharts.listener.PieChartOnValueSelectListener;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;


public class PieChart extends Fragment {
	private View view;
	private PieChartView pieChartSR,pieChartZC;  
    private PieChartData pieChardataSR,pieChardataZC;  
    private Data dataHelper;
	private int year,month;
	private int flag = 0;//用于区分支出和收入 0：支出      1：收入
    List<SliceValue> valuesSR = new ArrayList<SliceValue>(); 
    List<SliceValue> valuesZC = new ArrayList<SliceValue>(); 
    private int[] dataZC = new int[7];  
    private String[] dataZCS = new String[]{"餐饮","交通","购物","娱乐","居家","投资","人情"};
    private int[] dataSR = new int[7]; 
    private String[] dataSRS = new String[]{"工资","股票","奖金","利息","分红","补贴","报销"};   
    public static final int RED = Color.parseColor("#23AAE1");
    public static final int ORGEN = Color.parseColor("#DE7E2A");
    public static final int YELLOW = Color.parseColor("#B6D56E");
    public static final int GREEN = Color.parseColor("#F8951D");
    public static final int BLUE = Color.parseColor("#FADA8D");
    public static final int INDIGO = Color.parseColor("#CBCBCB");
    public static final int PURPLE = Color.parseColor("#DFA29D");
    public static final int[] COLORS = new int[] { RED, ORGEN, YELLOW, GREEN, BLUE, INDIGO, PURPLE };
    
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.kantubiao_piechart, container, false);
		pieChartSR = (PieChartView) view.findViewById(R.id.pie_chart_shouru);  
		pieChartSR.setOnValueTouchListener(selectListener_shouru);//设置点击事件监听  
		pieChartZC = (PieChartView) view.findViewById(R.id.pie_chart_zhichu);  
		pieChartZC.setOnValueTouchListener(selectListener_zhichu);//设置点击事件监听  
          
		initView();
        setPieChartData();  
        initPieChartSR();  
        initPieChartZC(); 
		return view;
	}
	
	 private void initView() {
		year = GetTime.getYear();
		month = GetTime.getMonth();
		dataHelper = new Data(this.getActivity());
		for(int i = 0;i < 7;i++){
			flag = 0;
			dataZC[i] = dataHelper.findCountByYearAndMonth(flag, year, month, dataZCS[i]);
			flag = 1;
			dataSR[i] = dataHelper.findCountByYearAndMonth(flag, year, month, dataSRS[i]);	
		}
	}

	/** 
     * 获取数据 
     */  
    private void setPieChartData(){  
        
        for (int i = 0; i < 7; ++i) {  
            SliceValue sliceValueSR = new SliceValue((float) dataSR[i], COLORS[i]);
            sliceValueSR.setLabel(dataSRS[i]);
            valuesSR.add(sliceValueSR);  
            SliceValue sliceValueZC = new SliceValue((float) dataZC[i], COLORS[i]);
            sliceValueZC.setLabel(dataZCS[i]);
            valuesZC.add(sliceValueZC); 
            
        }  
    }
    /** 
     * 初始化 
     */  
    private void initPieChartSR() {  
    	pieChardataSR = new PieChartData();  
    	pieChardataSR.setHasLabels(true);//显示表情  
    	pieChardataSR.setHasLabelsOnlyForSelected(false);//不用点击显示占的百分比  
    	pieChardataSR.setHasLabelsOutside(false);//占的百分比是否显示在饼图外面  
    	pieChardataSR.setHasCenterCircle(true);//是否是环形显示  
    	pieChardataSR.setValues(valuesSR);//填充数据  
    	pieChardataSR.setCenterCircleColor(Color.WHITE);//设置环形中间的颜色  
    	pieChardataSR.setCenterCircleScale(0.3f);//设置环形的大小级别  
    	pieChardataSR.setCenterText1("收入");//环形中间的文字1  
    	pieChardataSR.setCenterText1Color(Color.BLACK);//文字颜色  
    	pieChardataSR.setCenterText1FontSize(20);//文字大小  
    	
    	pieChartSR.setPieChartData(pieChardataSR);  
        pieChartSR.setValueSelectionEnabled(true);//选择饼图某一块变大  
        pieChartSR.setAlpha(0.9f);//设置透明度  
        pieChartSR.setCircleFillRatio(1f);//设置饼图大小  
          
    }  
    private void initPieChartZC(){
    	pieChardataZC = new PieChartData();  
    	pieChardataZC.setHasLabels(true);//显示表情  
    	pieChardataZC.setHasLabelsOnlyForSelected(false);//不用点击显示占的百分比  
    	pieChardataZC.setHasLabelsOutside(false);//占的百分比是否显示在饼图外面  
    	pieChardataZC.setHasCenterCircle(true);//是否是环形显示  
    	pieChardataZC.setValues(valuesZC);//填充数据  
    	pieChardataZC.setCenterCircleColor(Color.WHITE);//设置环形中间的颜色  
    	pieChardataZC.setCenterCircleScale(0.3f);//设置环形的大小级别  
    	pieChardataZC.setCenterText1("支出");//环形中间的文字1  
    	pieChardataZC.setCenterText1Color(Color.BLACK);//文字颜色  
    	pieChardataZC.setCenterText1FontSize(20);//文字大小  
           
        /**这里也可以自定义你的字体   Roboto-Italic.ttf这个就是你的字体库*/  
//      Typeface tf = Typeface.createFromAsset(this.getAssets(), "Roboto-Italic.ttf");  
//      data.setCenterText1Typeface(tf);  
          
        
        pieChartZC.setPieChartData(pieChardataZC);  
        pieChartZC.setValueSelectionEnabled(true);//选择饼图某一块变大  
        pieChartZC.setAlpha(0.9f);//设置透明度  
        pieChartZC.setCircleFillRatio(1f);//设置饼图大小  
    }
      
      
      
    /** 
     * 监听事件 
     */  
    private PieChartOnValueSelectListener selectListener_shouru = new PieChartOnValueSelectListener() {  
        @Override  
        public void onValueDeselected() {  
        	
        }  
          
        @Override  
        public void onValueSelected(int arg0, SliceValue value) {  
            Toast.makeText(PieChart.this.getActivity(),  value.getValue() + "元", Toast.LENGTH_SHORT).show();  
        }  
    }; 
    private PieChartOnValueSelectListener selectListener_zhichu = new PieChartOnValueSelectListener() {  
        @Override  
        public void onValueDeselected() {  
        	
        }  
          
        @Override  
        public void onValueSelected(int arg0, SliceValue value) {  
            Toast.makeText(PieChart.this.getActivity(),  value.getValue() + "元", Toast.LENGTH_SHORT).show();  
        }  
    }; 
}
